<template>
  <div>
    <div class="nav">
      <img src="image/log.png" alt="" />
      <span class="title">博客系统</span>
      <span class="spacer"></span>
      <a href="blog_list.html">主页</a>
      <a href="blog_edit.html">写博客</a>
      <a href="blog_login.html">注销</a>
    </div>

    <div class="container">
      <div class="container-left">
        <div class="card">
          <img src="image/head.jpg" alt="" />
          <h3>张三</h3>
          <div class="counter">
            <span>文章</span>
            <span>分类</span>
          </div>
          <div class="counter">
            <span>{{ blogs.length }}</span>
            <span>1</span>
          </div>
        </div>
      </div>

      <div class="container-right">
        <div v-for="(blog, index) in blogs" :key="index" class="blog">
          <div class="title">{{ blog.title }}</div>
          <div class="date">{{ blog.date }}</div>
          <div class="desc">{{ blog.desc }}</div>
          <a :href="blog.link" class="detail">查看全文 &gt;&gt;</a>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "BlogList",
  data() {
    return {
      blogs: [],
    };
  },
  created() {
    this.fetchBlogs();
  },
  methods: {
    fetchBlogs() {
      fetch('/api/blog/list')
        .then(res => res.json())
        .then(data => {
          this.blogs = data;
        });
    },
  },
};
</script>

<style scoped>
.blog {
  width: 100%;
  padding: 10px 20px;
}

.blog .title {
  text-align: center;
  font-size: 25px;
  font-weight: 700;
  padding-top: 10px;
  padding-bottom: 5px;
}

.blog .date {
  text-align: center;
  padding-bottom: 10px;
  color: grey;
}

.blog .desc {
  text-indent: 2em;
}

.blog .detail {
  display: block;
  width: 120px;
  color: grey;
  height: 30px;
  border: 2px solid grey;
  text-align: center;
  line-height: 30px;
  text-decoration: none;
  margin: 10px auto;
  transition: all 1s;
}

.blog .detail:hover {
  background-color: grey;
  color: white;
}

.nav {
  display: flex;
  align-items: center;
  padding: 10px;
  background-color: #f3f3f3;
}

.nav img {
  height: 40px;
}

.title {
  font-size: 24px;
  margin-left: 10px;
  font-weight: bold;
}

.spacer {
  flex: 1;
}

.nav a {
  margin: 0 10px;
  text-decoration: none;
  color: #333;
}

.container {
  display: flex;
  padding: 20px;
}

.container-left {
  width: 25%;
  padding-right: 20px;
}

.container-right {
  width: 75%;
}

.card {
  border: 1px solid #ccc;
  padding: 10px;
  text-align: center;
}

.card img {
  width: 100px;
  border-radius: 50%;
}

.counter {
  display: flex;
  justify-content: space-around;
  margin-top: 10px;
}
</style>
